//
//  WXBaMeiDaShanViewController.m
//  Big Show
//
//  Created by dlios on 15-5-18.
//  Copyright (c) 2015年 Big_Q. All rights reserved.
//

#import "WXBaMeiDaShanViewController.h"
#import "WXBaMeiDaShanCollectionViewCell.h"
#import "WXBaMeiLableViewController.h"
#import "XHNetworkHandle.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 667
@interface WXBaMeiDaShanViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableDictionary *mDic;
@property (nonatomic, retain) NSMutableArray *mArr;

@property (nonatomic, retain) UIImageView *img;
@end

@implementation WXBaMeiDaShanViewController


- (void)dealloc
{
    [_collectionView release];
    [_mDic release];
    [_mArr release];
    [_img release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _myTitle;
   
    self.navigationController.navigationBar.translucent = NO;
    
    [self createImageView];
    [self createCollectionView];
    [self getBaMeiData];

}


- (void)getBaMeiData
{
    [XHNetworkHandle getDataWithURL:_url cookie:nil completion:^(id result) {
        // result为请求接口的返回数据
        self.mDic = result;
        self.mArr = [_mDic objectForKey:@"list"];
        [_collectionView reloadData];
    }];
    
}


- (void)createImageView
{
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _img.image = [UIImage imageNamed:@"u=2916742164,3184761356&fm=21&gp=0.jpg"];
    [self.view addSubview:_img];
    [_img release];
}




// 创建collectionView
-(void)createCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

//    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
//    flowLayout.itemSize = CGSizeMake(160, 120);
//    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 100) collectionViewLayout:flowLayout];

    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTH* 15, HEIGHT* 15, WIDTH* 15, HEIGHT* 15);
    flowLayout.itemSize = CGSizeMake(WIDTH* 160, HEIGHT* 120);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) collectionViewLayout:flowLayout];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [flowLayout release];
    
    [_collectionView registerClass:[WXBaMeiDaShanCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

#pragma mark - 必须实现方法1
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _mArr.count;

}
#pragma mark - 必须实现方法2
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WXBaMeiDaShanCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
   [cell setDic:[_mArr objectAtIndex:indexPath.row]];
    return  cell;
}

#pragma mark - 点击详情
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WXBaMeiLableViewController *bameiVC = [[WXBaMeiLableViewController alloc] init];
    NSString *url = [NSString stringWithFormat:@"http://app.api.repaiapp.com/sx/songshijie/man/%@/get.php?type=%ld", _urll, indexPath.item];
    bameiVC.url = url;
    
    [self.navigationController pushViewController:bameiVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
